//
//  ViewController.m
//  AlbumDemo
//
//  Created by hejinhui on 2018/1/22.
//  Copyright © 2018年 hejinhui. All rights reserved.
//

#import "ViewController.h"
#import "AlbumView.h"
#import "UIView+Extension.h"
#import "AlbumModel.h"
#import "AlbumPhoto.h"
#import "AlbumItem.h"
#import "AlbumBaseView.h"
#import "AlbumCellModel.h"
#import "AlbumCellPhoto.h"
#import "AlbumCell.h"
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,AlbumMagDelegate>
@property(nonatomic,strong)NSMutableArray *dataSource;
@property(nonatomic,strong)AlbumView *ablumView;
@property(nonatomic,strong)AlbumCellModel *albumModel1;
@property(nonatomic,strong)AlbumBaseView *baseView;


@property(nonatomic,assign)NSInteger flag;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically
//    [self loadModel];
//    [self.view addSubview:self.ablumView];

    [self setUpNewView];
}

-(void)setUpNewView
{
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < 4; i++) {
        
        AlbumCellModel *model= [[AlbumCellModel alloc]init];
        model.title = @"11";
        model.isBtn = YES;
        
        UIImage *img = [UIImage imageNamed:@"IDCard_1"];
        
        AlbumCellPhoto *p = [[AlbumCellPhoto alloc]init];
        p.image = img;
        model.photos = @[p];
        [arr addObject:model];
        
    }
    
    AlbumBaseView *baseV = [[AlbumBaseView alloc]initWithAblum:arr.count column:2];
    baseV.albumDelegate  = self;
    baseV.frame = self.view.bounds;
    baseV.backgroundColor = [UIColor whiteColor];
    baseV.dataSource = [arr copy];
    [self.view addSubview:baseV];
    _baseView = baseV;
}

#pragma mark --
-(void)albumnDidSelected:(AlbumCell *)item albumModel:(AlbumCellModel *)albumModel albumView:(AlbumBaseView *)baseView
{
    _albumModel1 = albumModel;
    
    item.backgroundColor = [UIColor yellowColor];

//    [self takePhoto];
    NSLog(@"%@---",albumModel.photos);
   
}

-(NSMutableArray *)dataSource{
    
    if (!_dataSource) {
        
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


- (IBAction)addImage:(id)sender {
    
  
    
}

-(void)takePhoto
{
    // 判断当前的sourceType是否可用
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        // 实例化UIImagePickerController控制器
        UIImagePickerController * imagePickerVC = [[UIImagePickerController alloc] init];
        // 设置资源来源（相册、相机、图库之一）
        imagePickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        // 设置可用的媒体类型、默认只包含kUTTypeImage，如果想选择视频，请添加kUTTypeMovie
        // 如果选择的是视屏，允许的视屏时长为20秒
        imagePickerVC.videoMaximumDuration = 20;
        // 允许的视屏质量（如果质量选取的质量过高，会自动降低质量）
        imagePickerVC.videoQuality = UIImagePickerControllerQualityTypeHigh;
        // 设置代理，遵守UINavigationControllerDelegate, UIImagePickerControllerDelegate 协议
        imagePickerVC.delegate = self;
        // 是否允许编辑（YES：图片选择完成进入编辑模式）
        imagePickerVC.allowsEditing = YES;
        // model出控制器
        [self presentViewController:imagePickerVC animated:YES completion:nil];
    }
}

// 选择图片成功调用此方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
        
        // dismiss UIImagePickerController
        [self dismissViewControllerAnimated:YES completion:nil];
        // 选择的图片信息存储于info字典中
    NSLog(@"%@----flag--%ld", info,(long)self.flag);
    if (self.flag == 0) {
        
        AlbumCellPhoto * p = [[AlbumCellPhoto alloc]init];
        _albumModel1.photos = @[p];
    }
}
// 取消图片选择调用此方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
        
        // dismiss UIImagePickerController
        [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
